﻿using Framework.Common;
using System;
using System.Collections.Generic;

namespace BridgeShips
{
    /// <summary>
    /// 属性基类
    /// </summary>
    public class Attribute<Key>
    {
        private const int ACCURACY = 100;
        private Dictionary<Key, Value> m_Attributes;

        public Dictionary<Key, Value> Attributes
        {
            get
            {
                return m_Attributes;
            }
        }
        public Attribute()
        {
            m_Attributes = new Dictionary<Key, Value>();
        }
        public void RemoveValue(Key key)
        {
            if (m_Attributes.ContainsKey(key))
                m_Attributes.Remove(key);
        }
        public float GetFloat(Key key)
        {
            if (!m_Attributes.ContainsKey(key))
                return 0;
            return 1.0f * m_Attributes[key].CurrentValue / ACCURACY;
        }
        public int GetInt(Key key)
        {
            if (!m_Attributes.ContainsKey(key))
                return 0;
            return m_Attributes[key].CurrentValue;
        }
        public void Set(Key key, int value)
        {
            if (!m_Attributes.ContainsKey(key))
            {
                m_Attributes.Add(key, new Value(value));
                return;
            }
            m_Attributes[key].SetValue(value);
        }
        public void Set(Key key, float value)
        {
            if (!m_Attributes.ContainsKey(key))
            {
                m_Attributes.Add(key, new Value((int)(value * ACCURACY)));
                return;
            }
            m_Attributes[key].SetValue((int)(value * ACCURACY));
        }
        public void ChangeValue(Key key, int value)
        {
            if (!m_Attributes.ContainsKey(key)) return;
            m_Attributes[key].ChangeValue(value);
        }
        public void ChangeValue(Key key, float value)
        {
            if (!m_Attributes.ContainsKey(key)) return;
            int valueInt = (int)(value * ACCURACY);
            m_Attributes[key].ChangeValue(valueInt);
        }
        public void AddChangeListener(Key key, Action<int, int> callback)
        {
            if (!m_Attributes.ContainsKey(key)) return;
            m_Attributes[key].AddValueChangeListener(callback);
        }
        public void RemoveChangeListener(Key key, Action<int, int> callback)
        {
            if (!m_Attributes.ContainsKey(key)) return;
            m_Attributes[key].RemoveValueChangeListener(callback);
        }
        public void Release()
        {
            foreach (var item in m_Attributes)
            {
                item.Value.Release();
            }
            m_Attributes.Clear();
            m_Attributes = null;
        }
    }
}
